package abel.wlmr.client;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import abel.util.HexString;
import abel.wlmr.model.WlmrPacket;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class WlmrPacketEncoder extends MessageToByteEncoder<WlmrPacket> {

    private static final Logger log = LoggerFactory.getLogger(WlmrPacketEncoder.class);

    @Override
    protected void encode(ChannelHandlerContext ctx, WlmrPacket msg, ByteBuf out) throws Exception {
        try {
            msg.appendBuf(out);

            log.warn("Up {}: {}\nHex: {}",
                msg.getAfn(), msg, HexString.bytesToHexStringWithBlank(out.duplicate()));
        } catch (Exception ex) {
            log.error("Fail to encode message cuased by " + ex.getMessage(), ex);
        }
    }

}
